#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>

#include "css_document.h"
#include "css_parser.h"

using namespace std;

css_document *doc;
css_parser *parse;

int main(int argc, char **argv){

	if(argc != 2){
		printf("Usage: %s file\n",argv[0]);
		return -1;
	}
	FILE *fp;
	fp = fopen(argv[1],"r");			//open css file to test
	if (!fp){
		printf("Unable to open file\n");
		return -1;
    }
	fseek(fp,0,SEEK_END);
    unsigned int size = ftell(fp);				//find file size for the buffer
    fseek(fp,0,SEEK_SET);

	char *buffer = new char [size+1];			//allocate the buffer
	memset(buffer, 0, size);			//so it HAS to end in \0

	fread(buffer, size, 1, fp);		//read file to buffer
	fclose(fp);					//close file

	doc = new css_document;
	parse = new css_parser;

	parse->parse(doc,buffer);		//parse css document

	delete [] buffer;			//free memory

	css_property *prop;
	css_name *location;
	char *attribute;

	for(int x = 0;x < (int)doc->doc_properties.size();x++){
		prop = doc->doc_properties[x];
		
		location = prop->location->getvalue();

		cout << "Location:  ";
		if(location->id) cout << "ID: " << location->id << " ";
		if(location->clss) cout << "CLASS: " << location->clss << " ";
		if(location->tag) cout << "TAG: " << location->tag << " ";
		
		cout << "\n\n";
		

		for(int y = 0;y < (int)doc->doc_properties[x]->attrib.size();y++){
			attribute = prop->attrib[y]->getvalue();
			cout << "Attribute:" << attribute << "\n";

		}
		cout << "\n-------------------------\n";
	}

}
